<?php
class Shop_Model_Observer_AttacheProductToCategory extends Promotor_Observable_Observer_Abstract {
	public function update(Promotor_Observable_List $list, $productId = null) {
		$rq = $this->getRequest();
		if ((null === ($categories = $rq->getPost('categorytab')))) {
			$this->_setStatus(self::NULL);
			return;
		}
		if (is_array($productId)) {
			$productId = isset($productId['id'])
				? $productId['id']
				: current($productId);
		}
		if (null === $productId) {
			$this->_setStatus(self::NULL);
			return;
		}

		$hasCategory = new Shop_Model_HasCategory();
		$hasCategory->attacheProductToCategory($productId, (array) @$categories['tree']);

		$status = $hasCategory->getStatus();

		$this->_setStatus($status);
		$this->_setMessages($hasCategory->getMessages(false));
		$this->_exception = $hasCategory->getExceptions();
	}
}